package cn.wolfcode.web.Interceptor;

import cn.wolfcode.util.UserContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//      handler存放了控制器方法的处理类或者是静态资源的处理类
//      所以我们判断他是否是动态资源的类,如果不是就放行
        if(handler instanceof HandlerMethod){
           Object user = request.getSession().getAttribute(UserContext.USER_IN_SESSION);
           if(user == null){
               response.sendRedirect("/login.html");
               return false;
           }
       }
        return true;
    }
}
